<?php

include_once __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Config.php";
include_once "Database.php";

class Model
{

    protected $_db_handler;
    protected $_table;

    function __construct()
    {
        $this->_db_handler = Database::getDBHandler();
        $this->_db_handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->_db_handler->exec("SET CHARACTER SET utf8");
    }

    protected function insert($sql, array $data)
    {
        $q = $this->_db_handler->prepare($sql);

        foreach ($data as $k => $v) {
            $q->bindValue(':' . $k, $v);
        }

        $q->execute();
    }

    public function chunkBulkInsert($sql, $data)
    {
        foreach ($data as $chunks) {
            $subsql = $sql;
            $sqlParts = array();
            foreach ($chunks as $row) { //bind the values one by one
                $sqlPart = "(";
                $sqlPart .= $this->_db_handler->quote($row[0]);
                $sqlPart .= "," . $this->_db_handler->quote($row[1]);
                $sqlPart .= ",$row[2]";
                $sqlPart .= ",$row[3]";
                $sqlPart .= ",$row[4]";
                $sqlPart .= ",$row[5]";
                $sqlPart .= ",$row[6]";
                $sqlPart .= ",$row[7]";
                $sqlPart .= ",$row[8]";
                $sqlPart .= ",$row[9]";
                $sqlPart .= ",$row[10]";
                $sqlPart .= ",$row[11]";
                $sqlPart .= ")";
                $sqlParts[] = $sqlPart;
            }
            $subsql.= implode(",", $sqlParts) . ";";
            echo $subsql . "\n";
            $sth = $this->_db_handler->prepare($subsql);
            $sth->execute();
        }
    }

    public function insertNotProvided($sql, $data)
    {
        $data_chunks = array_chunk($data['rows'], 1000);
        foreach ($data_chunks as $chunks) {
            $subsql = $sql;
            $sqlParts = array();
            foreach ($chunks as $row) { //bind the values one by one
                $sqlPart = "(";
                $sqlPart .= $this->_db_handler->quote($row[0]);
                $sqlPart .= "," . $this->_db_handler->quote($row[1]);
                $sqlPart .= ",$row[2]";
                $sqlPart .= ",$row[3]";
                $sqlPart .= ")";
                $sqlParts[] = $sqlPart;
            }
            $subsql.= implode(",", $sqlParts) . ";";
            echo $subsql . "\n";
            $sth = $this->_db_handler->prepare($subsql);
            $sth->execute();
        }
    }

    public function bulkInsert($sql, $data)
    {

        $data_chunks = array_chunk($data['rows'], 1000);
        $this->chunkBulkInsert($sql, $data_chunks);
//        foreach ($data['rows'] as $row) { //bind the values one by one
//            $sqlPart = "(";
//            $sqlPart .= "'$row[0]'";
//            $sqlPart .= ",'$row[1]'";
//            $sqlPart .= ",$row[2]";
//            $sqlPart .= ",$row[3]";
//            $sqlPart .= ",$row[4]";
//            $sqlPart .= ",$row[5]";
//            $sqlPart .= ",$row[6]";
//            $sqlPart .= ",$row[7]";
//            $sqlPart .= ",$row[8]";
//            $sqlPart .= ",$row[9]";
//            $sqlPart .= ",$row[10]";
//            $sqlPart .= ",$row[11]";
//            $sqlPart .= ")";
//            $sqlParts[] = $sqlPart;
//        }
//        $sql.= implode(",", $sqlParts);
//        $sth = $this->_db_handler->prepare($sql);
//        $sth->execute();
    }

    protected function queryOne($sql, $params = array())
    {
        $sth = $this->_db_handler->prepare($sql);
        if ($params != array()) {
            foreach ($params as $key => $value) {
                $sth->bindValue(':' . $key, $value, PDO::PARAM_INT);
            }
        }
        $sth->execute();
        $results = $sth->fetchAll(PDO::FETCH_ASSOC);
        return $results;
    }

    protected function exeUpdateRank($update_query)
    {
        $data_chunks = array_chunk($update_query, 1000);
        foreach ($data_chunks as $chunks) {
            $subquery = "";
            foreach ($chunks as $chunk) {
                $subquery.=$chunk;
            }
            $sth = $this->_db_handler->prepare($subquery);
            $sth->execute();
        }
    }

    protected function escape($value)
    {
        return mysql_real_escape_string($value);
    }

}

